package priv.tuyou.bo;

import java.io.IOException;

import priv.tuyou.util.CommendUtil;

/**
 * CPU使用率
 * @author：涂有
 * @date 2017年1月4日 下午4:17:20
 */
public class CpuUsage {

	/**
	 * 用户时间
	 */
	private float userTime;
	
	/**
	 * 系统时间
	 */
	private float systemTime;
	
	/**
	 * 系统调整进程优先级的时候花费的时间
	 */
	private float niceTime;
	
	/**
	 * 空闲时间
	 */
	private float idleTime;
	
	/**
	 * 等待时间
	 */
	private float waitTime;
	
	/**
	 * 硬件中断处理时间
	 */
	private float hardIrqTime;
	
	/**
	 * 软件中断处理时间
	 */
	private float softIrqTime;
	
	/**
	 * 丢失时间
	 */
	private float stealTime;
	
	public static CpuUsage getInstance() throws IOException{
		
//		%Cpu(s):  0.6 us,  0.1 sy,  0.0 ni, 99.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
//		String result = "%Cpu(s):  0.6 us,  0.1 sy,  0.0 ni, 99.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st";
		String result = CommendUtil.exec("top -n 1 | grep %Cpu");
		result = result.substring(8).trim();
		String[] params = result.split(",");
		float userTime = Float.parseFloat(params[0].trim().split("\\s+")[0]);
		float systemTime = Float.parseFloat(params[1].trim().split("\\s+")[0]);
		float niceTime = Float.parseFloat(params[2].trim().split("\\s+")[0]);
		float idleTime = Float.parseFloat(params[3].trim().split("\\s+")[0]);
		float waitTime = Float.parseFloat(params[4].trim().split("\\s+")[0]);
		float hardIrqTime = Float.parseFloat(params[5].trim().split("\\s+")[0]);
		float softIrqTime = Float.parseFloat(params[6].trim().split("\\s+")[0]);
		float stealTime = Float.parseFloat(params[7].trim().split("\\s+")[0]);
		
		CpuUsage usage = new CpuUsage();
		usage.setUserTime(userTime);
		usage.setSystemTime(systemTime);
		usage.setNiceTime(niceTime);
		usage.setIdleTime(idleTime);
		usage.setWaitTime(waitTime);
		usage.setHardIrqTime(hardIrqTime);
		usage.setSoftIrqTime(softIrqTime);
		usage.setStealTime(stealTime);
		return usage;
	}

	public float getUserTime() {
		return userTime;
	}

	public void setUserTime(float userTime) {
		this.userTime = userTime;
	}

	public float getSystemTime() {
		return systemTime;
	}

	public void setSystemTime(float systemTime) {
		this.systemTime = systemTime;
	}

	public float getNiceTime() {
		return niceTime;
	}

	public void setNiceTime(float niceTime) {
		this.niceTime = niceTime;
	}

	public float getIdleTime() {
		return idleTime;
	}

	public void setIdleTime(float idleTime) {
		this.idleTime = idleTime;
	}

	public float getWaitTime() {
		return waitTime;
	}

	public void setWaitTime(float waitTime) {
		this.waitTime = waitTime;
	}

	public float getHardIrqTime() {
		return hardIrqTime;
	}

	public void setHardIrqTime(float hardIrqTime) {
		this.hardIrqTime = hardIrqTime;
	}

	public float getSoftIrqTime() {
		return softIrqTime;
	}

	public void setSoftIrqTime(float softIrqTime) {
		this.softIrqTime = softIrqTime;
	}

	public float getStealTime() {
		return stealTime;
	}

	public void setStealTime(float stealTime) {
		this.stealTime = stealTime;
	}

	@Override
	public String toString() {
		return "CpuUsage [userTime=" + userTime + ", systemTime=" + systemTime + ", niceTime=" + niceTime
				+ ", idleTime=" + idleTime + ", waitTime=" + waitTime + ", hardIrqTime=" + hardIrqTime
				+ ", softIrqTime=" + softIrqTime + ", stealTime=" + stealTime + "]";
	}
}
